<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button type="info" slot="title" icon="search" round class="home-search" to="/search">搜索</van-button>
    </van-nav-bar>
    <!-- 标签页 -->
    <!-- animated 属性可以开启切换标签内容时的转场动画 -->
    <!-- swipeable 属性可以开启滑动切换标签页 -->
    <van-tabs v-model="active" animated swipeable class="tabs-bar">
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <article-list :item="item"></article-list>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isShowPopup = true">
        <i class="iconfont icongengduo"></i>
      </div>
    </van-tabs>
    <!-- 弹出层 -->
    <van-popup v-model="isShowPopup" closeable close-icon-position="top-left" position="bottom" :style="{ height: '100%' }" class="home-popup">
      <channel-edit :channels="channels" :active="active" @target-page="changeActive"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserchannels } from '@/api/user'
import articleList from './components/article-list'
import channelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getUser } from '@/utils/storage'
export default {
  name: 'homeIndex',
  data() {
    return {
      active: 0,
      channels: [], // 频道数据
      isShowPopup: false // 弹出层的显示状态
    }
  },
  created() {
    this.userChannels()
  },
  mounted() {},
  methods: {
    // 获取我的频道
    async userChannels() {
      let channels = []
      try {
        if (this.user) {
          // 以登录
          const { data } = await getUserchannels()
          channels = data.data.channels
        } else {
          const channel = this.channels = getUser('channel')
          if (channel) {
            // 未登录(有历史记录)
            channels = channel
          } else {
            // 未登录(无历史记录)
            const { data } = await getUserchannels()
            channels = data.data.channels
          }
        }
      } catch (err) {
        this.$toast.fail('获取我的频道失败')
      }
      this.channels = channels
    },
    changeActive(index, flag) {
      this.active = index
      this.isShowPopup = flag
    }
  },
  components: {
    articleList,
    channelEdit
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style scoped lang="less">
.home-container {
  .home-search {
    width: 555px;
    height: 64px;
    font-size: 28px;
    color: #fff;
    background-color: #5babfb;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  /deep/.tabs-bar {
    padding-top: 180px;
    height: 83px;
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      color: #777;
      font-size: 28px;
    }
    .van-tab--active {
      font-size: 30px;
      color: #333;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
      border-radius: 3px;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 83px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 83px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      opacity: 0.902;
      .iconfont {
        font-size: 33px;
        color: #333;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
    // .home-popup {
    //   background-color: #fff;
    // }
  }
}
</style>
